package com.webbuilder.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.lang3.RandomUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.webbuilder.common.Var;
import com.webbuilder.utils.DateUtil;
import com.webbuilder.utils.DbUtil;
import com.webbuilder.utils.LogUtil;
import com.webbuilder.utils.StringUtil;
import com.webbuilder.utils.SysUtil;

public class BrushVotesTask implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		Connection conn = null;
		PreparedStatement st = null;
		PreparedStatement updateSt = null;
		PreparedStatement begin_random_st = null;
		PreparedStatement end_random_st = null;
		PreparedStatement start_ids_st = null;
		ResultSet begin_random_rs = null;
		ResultSet end_random_rs = null;
		ResultSet start_ids_rs = null;
		try {
			conn = DbUtil.getConnection();
			// 明星ids
			String insertStartRecordSql="insert into champion_start_currency_record(id,user_id,add_date,start_id,energy_currency,create_by,create_name,create_date,update_by,update_name,update_date,remarks)";
			insertStartRecordSql+=" values(?,'admin',now(),?,?,'admin','admin',now(),'admin','admin',now(),'明星加能')";
			st = conn.prepareStatement(insertStartRecordSql);
			String updateSql="update champion_start_list set energy_currency=energy_currency+?,brush_currency=brush_currency+? where id=? ";
			updateSt = conn.prepareStatement(updateSql);
			String startIdsSql = "select * from wb_var where VAR_NAME = 'start_ids'";
			start_ids_st = conn.prepareStatement(startIdsSql);
			start_ids_rs = start_ids_st.executeQuery();
			String start_ids = "";
			if (start_ids_rs.next()) {
				
				start_ids = start_ids_rs.getString("VAR_VALUE");
				if (!StringUtil.isEmpty(start_ids)) {
					String[] startIds=start_ids.split(",");
					for(int i=0;i<startIds.length;i++){
					String beginRandomSql = "select * from wb_var where VAR_NAME = 'begin_random'";
					begin_random_st = conn.prepareStatement(beginRandomSql);
					begin_random_rs = begin_random_st.executeQuery();
					int begin_random = 0;
					if (begin_random_rs .next()) {
						if(!StringUtil.isEmpty(begin_random_rs.getString("VAR_VALUE"))){
							begin_random = begin_random_rs.getInt("VAR_VALUE");
						}
						String endRandomSql = "select * from wb_var where VAR_NAME = 'end_random'";
						end_random_st = conn.prepareStatement(endRandomSql);
						end_random_rs = end_random_st.executeQuery();
						int end_random=0;
						if(end_random_rs .next() ){
							if(!StringUtil.isEmpty(end_random_rs.getString("VAR_VALUE"))){
								end_random = end_random_rs.getInt("VAR_VALUE");
							}
							if(end_random >0){
								int random=RandomUtils.nextInt(begin_random, end_random+1);
								st.setString(1, SysUtil.getUUID());
								st.setString(2, startIds[i]);
								st.setString(3, random+"");
								st.addBatch();
								updateSt.setInt(1, random);
								updateSt.setInt(2, random);
								updateSt.setString(3, startIds[i]);
								updateSt.addBatch();
								updateSt.executeUpdate();

							}
						}
					}
				}
					st.executeBatch();
			}
			}
		} catch (Throwable e) {
			System.out.println(e);
			DbUtil.closeConnection(conn, true);
		} finally {
			DbUtil.closeStatement(st);
			DbUtil.closeStatement(updateSt);
			DbUtil.closeStatement(begin_random_st);
			DbUtil.closeStatement(end_random_st);
			DbUtil.closeStatement(start_ids_st);
			DbUtil.closeResultSet(begin_random_rs);
			DbUtil.closeResultSet(end_random_rs);
			DbUtil.closeResultSet(end_random_rs);
			DbUtil.closeConnection(conn);
			DbUtil.closeConnection(conn, false);
		}
	}
}